package com.wanlian.candy.controller;

import com.wanlian.candy.controller.session.UserSessionInfo;
import com.wanlian.common.ConstantCode;
import com.wanlian.common.PageCondition;
import com.wanlian.entity.DeviceData;
import com.wanlian.error.ErrorCode;
import com.wanlian.json.JsonResult;
import com.wanlian.json.JsonStringUtil;
import com.wanlian.model.ResultModel;
import com.wanlian.service.DeviceDataServices;
import com.wanlian.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/devicedata")
public class DeviceDataController {


    @Autowired
    public DeviceDataServices deviceDataServices;


    /**
     * 添加设备数据
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/add", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String add(@RequestBody String data) {
        DeviceData deviceData = JsonStringUtil.readValue(data, DeviceData.class);
        ResultModel resultModel = deviceDataServices.doAdd(deviceData);
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 添加设备数据
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/add/list", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String addList(@RequestBody String data) {
        List<DeviceData> deviceData = JsonStringUtil.readValueByList(data, DeviceData.class);
        List<DeviceData> deviceData1= deviceDataServices.editBatch(deviceData);
        if(deviceData.size()>0){
            deviceDataServices.doInsertsBatch(deviceData1);
        }
        return new JsonResult().Serailze(new ResultModel(true));
    }


    /**
     * 查看路由数据列表
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/list", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String listByCondition(@RequestBody String data, HttpSession session) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (user == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        ResultModel resultModel = deviceDataServices.queryByParam(pageCondition);
        return new JsonResult().Serailze(resultModel);
    }

    @RequestMapping(value = "/all/today", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String listByCondition() {
        String date = DateUtil.currentDatetime("yyyy-MM-dd");
        ResultModel resultModel = new ResultModel();
        List<DeviceData> datas = deviceDataServices.queryAllData(date);
        resultModel.setObject(JsonStringUtil.writeValueAsString(datas));
        return new JsonResult().Serailze(resultModel);
    }


}
